package org.bbb.library1.views;

/*******************************************************************************
 * Copyright (c) 2007 Finalist IT Group B.V.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Rob Schellhorn - initial API and implementation
 *******************************************************************************/


import org.bbb.library1.model.Library;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

//import com.finalist.rcp.core.Library;

/**
 * @author Rob Schellhorn
 */
public class ExplorerContentProvider implements ITreeContentProvider, Observer {

   private Viewer viewer;

   /*
    * @see org.eclipse.jface.viewers.IContentProvider#dispose()
    */
   public void dispose() {/* Nothing to dispose of */}

   /*
    * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
    */
   public Object[] getChildren(Object parentElement) {
      return new Object[] {};
   }

   /*
    * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
    */
   public Object[] getElements(Object inputElement) {
      if (inputElement instanceof Library) {
         return Library.class.cast(inputElement).getBooks().toArray();
      }
      return new Object[] {};
   }

   /*
    * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
    */
   public Object getParent(Object element) {
      return null;
   }

   /*
    * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
    */
   public boolean hasChildren(Object element) {
      return false;
   }

   /*
    * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
    *      java.lang.Object, java.lang.Object)
    */
   public void inputChanged(final Viewer viewer, Object oldInput, Object newInput) {
      if (oldInput instanceof Observable) {
         // no long observe the old input
         Observable.class.cast(oldInput).deleteObserver(this);
         this.viewer = null;
      }

      if (newInput instanceof Observable) {
         // start observing the new input
         this.viewer = viewer;
         Observable.class.cast(newInput).addObserver(this);
      }
   }

   /*
    * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
    */
   public void update(Observable o, Object arg) {
      viewer.refresh();
   }
}